package com.ht.medicaltreatment.activity

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.view.View
import com.blankj.utilcode.util.LogUtils
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.utils.NFCUtils
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.utils.SoundUtils
import com.ht.medicaltreatment.utils.UIDUitls
import kotlinx.android.synthetic.main.activity_blood_speciment_collection_layout.btnBack
import kotlinx.android.synthetic.main.activity_blood_speciment_collection_layout.btnStart
import kotlinx.android.synthetic.main.activity_blood_speciment_collection_layout.imgFormRecord
import kotlinx.android.synthetic.main.activity_blood_speciment_collection_layout.imgUserInfo
import kotlinx.android.synthetic.main.activity_blood_speciment_collection_layout.llScan
import kotlinx.android.synthetic.main.activity_blood_speciment_collection_layout.tvInfo
import kotlinx.android.synthetic.main.activity_blood_speciment_collection_layout.tvRecord

/**
 * GLP 血样采集
 */
class BloodSpecimenCollectionMainActivity:BaseActivity(), View.OnClickListener {

    override fun getLayoutId() = R.layout.activity_blood_speciment_collection_layout

    private var isNFC = false
    private var nfcEnable = false

    override fun init() {
        setTitle("血样采集")

        imgFormRecord.setOnClickListener(this)
        tvRecord.setOnClickListener(this)
        imgUserInfo.setOnClickListener(this)
        tvInfo.setOnClickListener(this)
        btnStart.setOnClickListener(this)
        btnBack.setOnClickListener(this)

        initScan()
    }

    private fun initScan() {
        if(Build.MODEL == "V9100"){
            ScanUtils.init{
                if(!it){
                    btnStart.isEnabled = false
                    btnStart.text = "初始化失败"
                    btnStart.setBackgroundColor(getColor(R.color.colorTextGrayLight))
                }
            }
        }else{
            isNFC = packageManager.hasSystemFeature(PackageManager.FEATURE_NFC)
            if(isNFC){
                NFCUtils(this)
            }
        }
    }

    override fun onClick(v: View?) {
        when(v){
            //表单记录
            imgFormRecord,
            tvRecord -> {
                startActivity(FormRecordActivity::class.java)
            }

            //受试动物信息
            imgUserInfo,
            tvInfo -> {
                startActivity(AnimalInfoActivity::class.java)
            }

            //关闭扫描
            btnBack -> {
                llScan.visibility = View.GONE
                ScanUtils.close()
                nfcEnable = false
            }

            //开始识别
            btnStart -> {
                if(true){
                    val intent = Intent(this,BloodSpecimenCollectionDetailActivity::class.java)
                    startActivity(intent)
                }
                llScan.visibility = View.VISIBLE
                if(Build.MODEL == "V9100"){
                    ScanUtils.open {
                        ScanUtils.close()
                        SoundUtils.playSound(SoundUtils.SOUND_SUCCESS)
                        ScanUtils.resetItem(it)
                        llScan.visibility = View.GONE
                        var uid = it
                        if(uid.length != 8){
                            uid = UIDUitls.sort(uid)
                        }
                        val intent = Intent(this,BloodSpecimenCollectionDetailActivity::class.java)
                        intent.putExtra("uid",uid)
                        startActivity(intent)
                    }
                }else{
                    nfcEnable = true
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if(!isNFC)return
        //开启前台调度系统
        NFCUtils.mNfcAdapter.enableForegroundDispatch(this,NFCUtils.mPendingIntent,NFCUtils.mIntentFilter,
            NFCUtils.mTechList)
    }

    override fun onPause() {
        super.onPause()
        if(!isNFC)return
        //关闭前台调度系统
        NFCUtils.mNfcAdapter.disableForegroundDispatch(this)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        if(!isNFC || !nfcEnable)return
        //当该Activity接收到NFC标签时，运行该方法
        //调用工具方法，读取NFC数据
        var uid: String = NFCUtils.readNFCId(intent)
        LogUtils.i(uid)
        nfcEnable = false
        llScan.visibility = View.GONE
        if(uid.length != 8){
            uid = UIDUitls.sort(uid)
        }
        val intent = Intent(this,BloodSpecimenCollectionDetailActivity::class.java)
        intent.putExtra("uid",uid)
        startActivity(intent)
    }
}